﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.IO;
namespace Workshop
{
    public partial class FormCoursetime : Form
    {
        public String CurrentFirst;
        public String CurrentSecond;
        public String CurrentThird;
        public int CurrentIndex;
        public String CourseID;
        public String Instructor;
        public FormCoursetime()
        {
            InitializeComponent();
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void FormCoursetime_Load(object sender, EventArgs e)
        {
            updateGridView();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        public Boolean timeCheck(String box)  //This is to check if user input the right-acceptabl time for the course.
        {
            int firstA;
            int firstAmin;
            int firstB;
            int firstBmin;
            String firstTime = box;
            int firstNum = firstTime.IndexOf(" ");
            int indexMao = firstTime.IndexOf(":");
            int.TryParse(firstTime.Substring(indexMao + 1, 2), out firstAmin);
            if (indexMao - firstNum == 3)
            {
                int.TryParse(firstTime.Substring(firstNum+1, 2), out firstA); //get the first time from the format, like Wed 10:00~11:00, then here we get 10.
            }
            else
            {
                int.TryParse(firstTime.Substring(firstNum+1, 1), out firstA); //this is we can get number from for example Wed 8:00~9:00, then we get 8 here.
            }
            int indexBolan = firstTime.IndexOf("~");
            String halfString = firstTime.Substring(indexBolan);
            int IndexOfSecondM = halfString.IndexOf(":");
            int IndexOFSecondB = halfString.IndexOf("~");  // here is for getting another number of the time.
            int.TryParse(halfString.Substring(IndexOfSecondM + 1, 2), out firstBmin);
            if (IndexOfSecondM - IndexOFSecondB == 3)
            {
                int.TryParse(halfString.Substring(IndexOFSecondB + 1, 2), out firstB); //here, we get 9 from Wed 8:00~9:00.
            }
            else 
            {
                int.TryParse(halfString.Substring(IndexOFSecondB + 1, 1), out firstB);// we get 11 from Wed 10:00~11:00.
            }

            if ((firstB*60+firstBmin) - (firstA*60+firstAmin) == 50) // here do the calculation if the time for one course is 50 minutes or not.
                return true; //if it is, then return true.
            else
                return false;  //else return false.
        }

        

        public Boolean CheckTimeClash(String InstructorID, String first, String second, String third)
        {  //here is used to check the time clash for the instructor. We need to check first and second, and third these three parameters if they have time clash with the instructor.
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            DataTable dt = new DataTable();
            String path = "//Courses/CourseInfo/Course/InstructorID";
            XmlNodeList nodes=doc.SelectNodes(path);
            DataColumn col1 = new DataColumn("first", typeof(string));
            DataColumn col2= new DataColumn("second", typeof(string));
            DataColumn col3 = new DataColumn("third", typeof(string));
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            dt.Columns.Add(col3);
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].InnerText == InstructorID)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = nodes[i].ParentNode.ChildNodes[0].Attributes[0].Value.ToString();
                    dr[1] = nodes[i].ParentNode.ChildNodes[0].Attributes[1].Value.ToString();
                    dr[2] = nodes[i].ParentNode.ChildNodes[0].Attributes[2].Value.ToString();
                    dt.Rows.Add(dr);
                }
            }  // we put the Instructor schedule into a table.
            if (dt.Rows.Count == 0)
                return false;
            for(int i=0;i<dt.Rows.Count;i++)
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if ((first == dt.Rows[i][j].ToString()&&first!=CurrentFirst) || (second == dt.Rows[i][j].ToString()&&second!=CurrentSecond) || (third == dt.Rows[i][j].ToString()&&third!=CurrentThird))
                    {
                        return true;
                    }
                } // And the first and second and third will do comparation with the content in this table. In order to check time clash.
            return false;
        }

        private void okbutton_Click(object sender, EventArgs e)
        {
            if (firstbox.Text.IndexOf(" ") > -1 && firstbox.Text.IndexOf("~") > -1&&firstbox.Text.IndexOf(":") > -1)
            {
                if (firstbox.Text.IndexOf("Mon") > -1 || firstbox.Text.IndexOf("Tue") > -1 || firstbox.Text.IndexOf("Wed") > -1 || firstbox.Text.IndexOf("Thu") > -1 || firstbox.Text.IndexOf("Fri") > -1)
                {
                    if (secondbox.Text.IndexOf(" ") > -1 && secondbox.Text.IndexOf("~") > -1 && secondbox.Text.IndexOf(":") > -1)
                        if (secondbox.Text.IndexOf("Mon") > -1 || secondbox.Text.IndexOf("Tue") > -1 || secondbox.Text.IndexOf("Wed") > -1 || secondbox.Text.IndexOf("Thu") > -1 || secondbox.Text.IndexOf("Fri") > -1)
                        {
                            if (thirdbox.Text.IndexOf(" ") > -1 && thirdbox.Text.IndexOf("~") > -1 && thirdbox.Text.IndexOf(":") > -1)
                                if (thirdbox.Text.IndexOf("Mon") > -1 || thirdbox.Text.IndexOf("Tue") > -1 || thirdbox.Text.IndexOf("Wed") > -1 || thirdbox.Text.IndexOf("Thu") > -1 || thirdbox.Text.IndexOf("Fri") > -1)
                                {  // This for is used to check if the format of the inputting time is right or not.
                                    XmlDocument doc = new XmlDocument();
                                    doc.Load("Course.xml");
                                    String path = "//CourseID";
                                    XmlNodeList nodes = doc.SelectNodes(path);
                                    for (int i = 0; i < nodes.Count; i++)
                                    {
                                        if (nodes[i].InnerText == CourseID)
                                        {
                                            String InstructorID=nodes[i].ParentNode.ChildNodes[6].InnerText;
                                            String Instructor=nodes[i].ParentNode.ChildNodes[6].Attributes[0].Value.ToString();
                                            if (CheckTimeClash(InstructorID, firstbox.Text, secondbox.Text, thirdbox.Text)) //check if is has time clash ot not.
                                            {
                                                MessageBox.Show("there are time clash with teacher: " + Instructor, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                                return;
                                            }
                                            else 
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show("you must input a correct format of course time, like 'Wed 8:00~8:50'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return;
                                }
                            else
                            {
                                MessageBox.Show("you must input a correct format of course time, like 'Wed 8:00~8:50'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                        }
                        else
                        {
                            MessageBox.Show("you must input a correct format of course time, like 'Wed 8:00~8:50'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    else
                    {
                        MessageBox.Show("you must input a correct format of course time, like 'Wed 8:00~8:50'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                else 
                {
                    MessageBox.Show("you must input a correct format of course time, like 'Wed 8:00~8:50'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

            }
            else
            {
                MessageBox.Show("you must input a correct format of course time, like 'Wed 8:00~8:50'", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (firstbox.Text == secondbox.Text || firstbox.Text == thirdbox.Text || secondbox.Text == thirdbox.Text)
            {
                MessageBox.Show("Time duplication happen", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {
                if (!timeCheck(firstbox.Text))
                {
                    MessageBox.Show("Wrong format time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!timeCheck(secondbox.Text))
                {
                    MessageBox.Show("Wrong format time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (!timeCheck(thirdbox.Text))
                {
                    MessageBox.Show("Wrong format time", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                
                else
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load("Course.xml");
                    String path = "//Courses/CourseInfo/Course/CourseID";
                    XmlNodeList nodes = doc.SelectNodes(path);
                    for (int i = 0; i < nodes.Count; i++)
                    {
                        if (nodes[i].InnerText == CourseID)
                        {
                            nodes[i].ParentNode.ChildNodes[0].Attributes[0].Value = firstbox.Text;
                            nodes[i].ParentNode.ChildNodes[0].Attributes[1].Value = secondbox.Text;
                            nodes[i].ParentNode.ChildNodes[0].Attributes[2].Value = thirdbox.Text;
                            doc.Save("Course.xml");
                        }
                        else
                        {
                            continue;
                        }
                    }

                }// if everything is ok then we store the information into "Course.xml"
                updateGridView();
            }
        }

        public static void SetIDWid(DataGridView dataGridView1)
        {
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                if (dataGridView1.Columns[i].HeaderCell.Value.ToString() == "ID")
                {
                    dataGridView1.Columns[i].Width = 90;
                    dataGridView1.Columns[i].Resizable = DataGridViewTriState.False;
                    break;
                }
            }
        }  //Set the width for the ID column.

        public void updateGridView()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            String path = "//Courses/CourseInfo/Course/CourseID";
            DataTable dt = new DataTable();
            XmlNodeList nodes = doc.SelectNodes(path);
            DataColumn col0 = new DataColumn("ID", typeof(string));
            DataColumn col1 = new DataColumn("Course", typeof(string));
            DataColumn col2 = new DataColumn("Instructor", typeof(string));
            DataColumn col3 = new DataColumn("FirstTime", typeof(string));
            DataColumn col4 = new DataColumn("SecondTime", typeof(string));
            DataColumn col5 = new DataColumn("ThirdTime", typeof(string));
            dt.Columns.Add(col0);
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);
            dt.Columns.Add(col3);
            dt.Columns.Add(col4);
            dt.Columns.Add(col5);
            for (int i = 0; i < nodes.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = nodes[i].InnerText;
                dr[1] = nodes[i].ParentNode.ChildNodes[1].InnerText;
                dr[2] = nodes[i].ParentNode.ChildNodes[6].Attributes[0].Value.ToString();
                dr[3] = nodes[i].Attributes[0].Value.ToString();
                dr[4] = nodes[i].Attributes[1].Value.ToString();
                dr[5] = nodes[i].Attributes[2].Value.ToString();
                dt.Rows.Add(dr);
            }
            dataGridView2.DataSource = dt;
            SetIDWid(dataGridView2);
        }//update the DataGridView.

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView2.CurrentRow.Cells[2].Value.ToString() != "")
            {
                courseName.Text = dataGridView2.CurrentRow.Cells[1].Value.ToString();
                firstbox.Text = dataGridView2.CurrentRow.Cells[3].Value.ToString();
                secondbox.Text = dataGridView2.CurrentRow.Cells[4].Value.ToString();
                thirdbox.Text = dataGridView2.CurrentRow.Cells[5].Value.ToString();
                CourseID = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                CurrentFirst = firstbox.Text;
                CurrentSecond = secondbox.Text;
                CurrentThird = thirdbox.Text;
                firstbox.ReadOnly = false;
                secondbox.ReadOnly = false;
                thirdbox.ReadOnly = false;
            }
            else
            {
                courseName.Text = dataGridView2.CurrentRow.Cells[1].Value.ToString();
                firstbox.Text = "";
                secondbox.Text = "";
                thirdbox.Text = "";
                firstbox.ReadOnly = true;
                secondbox.ReadOnly = true;
                thirdbox.ReadOnly = true;
            }
        }//Everytime the user click the row then the some information needs to store, so we can use it for the later update.

        private void tAArrangementToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            FormTAarrangement Form = new FormTAarrangement();
            this.Hide();
            Form.Show();
        } //jump to another page

        private void courseName_TextChanged(object sender, EventArgs e)
        {

        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Course.xml");
            String path = "//Courses/CourseInfo/Course/CourseID";
            XmlNodeList nodes = doc.SelectNodes(path);
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].InnerText == CourseID)
                {
                    nodes[i].Attributes[0].Value = "";
                    nodes[i].Attributes[1].Value = "";
                    nodes[i].Attributes[2].Value = "";
                }
            }
            doc.Save("Course.xml");
            MessageBox.Show("Delete time successfully","Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            firstbox.Text = "";
            secondbox.Text = "";
            thirdbox.Text = "";
            updateGridView();
        }// delete the time for the course, as the user may input the wrong time.

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void courseTimeToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            
        }

        private void tAArrangementToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormTAarrangement form = new FormTAarrangement();
            this.Hide();
            form.Show();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            FormHeadPixel headPixel = new FormHeadPixel();
            headPixel.Show();
            this.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
